//
//  NQProvinceSelectView.m
//  ZLFastDoctor
//
//  Created by 李岚清 on 16/5/23.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "NQProvinceSelectView.h"
#import "NQPrivence.h"
#import "NQProvinceCell.h"

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>
//重用标识
static NSString *provinceSelectViewIdentifier = @"provinceSelectViewIdentifier";

@interface NQProvinceSelectView ()<UITableViewDataSource,UITableViewDelegate>
/* 设置类型 *///fengexian
@property (nonatomic, strong) UILabel *typeLabel;
/* pierView */
@property (nonatomic, strong) UITableView *provinceView;
//@property (nonatomic, strong) UIView *provinceView;
/* 取消按钮 */
@property (nonatomic, strong) UIButton *cancelBtn;
/* 提交按钮 */
@property (nonatomic, strong) UIButton *postBtn;
/* 分界线 */
@property (nonatomic, strong) UIView *boundLine;

/**
 *  省份数据源
 */
@property (nonatomic, strong) NSArray *privences;
/**
 *  记录选中cell的索引
 */
@property (nonatomic, weak) NSIndexPath *selectedCellIndex;

@end

@implementation NQProvinceSelectView
#pragma mark - 懒加载数据源
- (NSArray *)privences{
    if (!_privences) {
        //取出plist文件中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        NSArray *arrDicts = [NSArray arrayWithContentsOfFile:path];
        //定义空数组
        NSMutableArray *arrModel = [NSMutableArray array];
        //遍历字典转模型
        for (NSDictionary *dict in arrDicts) {
            NQPrivence *model = [NQPrivence privenceWithDict:dict];
            [arrModel addObject:model];
        }
        _privences = arrModel;
    }
    return _privences;
}

#pragma mark - 按钮点击事件
//取消
- (void)cancelPick{
    [self removeFromSuperview];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(provinceSelectViewDidClickProvince:provinceName:)]) {
        [self.delegate provinceSelectViewDidClickProvince:NQProvinceBtnTypeCancel provinceName:nil];
    }
}

//提交
- (void)post{
    [self removeFromSuperview];
    //调用代理方法
    if ([self.delegate respondsToSelector:@selector(provinceSelectViewDidClickProvince:provinceName:)]) {
        //取出选中的cell对应的模型
        NQPrivence *province = self.privences[self.selectedCellIndex.row];
        //传递选中的省份名字
        [self.delegate provinceSelectViewDidClickProvince:NQProvinceBtnTypePost provinceName:province.name];
    }
}

#pragma mark - 构造方法

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupAttrabute];
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupAttrabute];
        [self setupUI];
    }
    return self;
}

//设置frame
- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    //设置frame
    self.frame = CGRectMake(NQScreenW*0.1, NQScreenH*0.2, NQScreenW*0.8, 300);
}

//属性设置
- (void)setupAttrabute{
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
}

//添加子控件及约束
- (void)setupUI{
    //添加到view自身上
    [self addSubview:self.typeLabel];
    [self addSubview:self.provinceView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.postBtn];
    [self addSubview:self.boundLine];
    //设置约束
    [self.typeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).equalTo(10);
        make.centerX.equalTo(self);
    }];
    [self.provinceView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.bottom);
        make.centerX.equalTo(self);
        make.height.equalTo(260);
        make.width.equalTo(220);
    }];
    [self.cancelBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.provinceView.bottom).offset(10);
        make.bottom.equalTo(self).equalTo(-10);
        make.width.height.equalTo(self.postBtn);
        make.left.equalTo(self);
    }];
    [self.postBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.provinceView.bottom).offset(10);
        make.bottom.equalTo(self).equalTo(-10);
        make.left.equalTo(self.cancelBtn.right);
        make.right.equalTo(self);
    }];
    [self.boundLine makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.postBtn).equalTo(-10);
        make.width.equalTo(1);
        make.top.equalTo(self.postBtn).equalTo(5);
        make.right.equalTo(self.postBtn.left);
    }];
    
}
#pragma mark - UITableView数据源方法
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.privences.count;
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.创建单元格
    NQProvinceCell *cell = [tableView dequeueReusableCellWithIdentifier:provinceSelectViewIdentifier forIndexPath:indexPath];
    
    //2.赋值模型数据
    cell.privence = self.privences[indexPath.row];
    //持续进行判断
    if (self.selectedCellIndex == indexPath && self.selectedCellIndex != nil) {
        cell.isSelected = YES;
    }else{
        cell.isSelected = NO;
    }
    
    //3.返回单元格
    return cell;
}


#pragma mark - UITableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

//选中了某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取出上次选中的cell
    if (self.selectedCellIndex != nil) {
        NQProvinceCell *preProvinceCell = [tableView cellForRowAtIndexPath:self.selectedCellIndex];
        preProvinceCell.isSelected = NO;
    }
    
    //当前选中的cell
    NQProvinceCell *provinceCell = [tableView cellForRowAtIndexPath:indexPath];
    provinceCell.isSelected = YES;
    self.selectedCellIndex = indexPath;
}

#pragma mark - 懒加载子控件
- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] init];
        _typeLabel.text = @"省份";
        _typeLabel.textColor = NQColor(24, 188, 187);
        _typeLabel.font = [UIFont systemFontOfSize:16];
    }
    return _typeLabel;
}

- (UITableView *)provinceView{
    if (!_provinceView) {
        _provinceView = [[UITableView alloc] init];
        _provinceView.delegate = self;
        _provinceView.dataSource = self;
        [_provinceView registerClass:[NQProvinceCell class] forCellReuseIdentifier:provinceSelectViewIdentifier];
    }
    return _provinceView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_cancelBtn addTarget:self action:@selector(cancelPick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIView *)boundLine{
    if (!_boundLine) {
        _boundLine = [[UIView alloc] init];
        _boundLine.backgroundColor = [UIColor grayColor];
    }
    return _boundLine;
}

- (UIButton *)postBtn{
    if (!_postBtn) {
        _postBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_postBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_postBtn setTitleColor:NQColor(24, 188, 187) forState:UIControlStateNormal];
        _postBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_postBtn addTarget:self action:@selector(post) forControlEvents:UIControlEventTouchUpInside];
    }
    return _postBtn;
}



@end
